home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
bioinformatics.org
/
bioinformatics.org_software.tar
/
www.bioinformatics.org
/
download
/
ecell2
/
ecell220setup.exe
/
{app}
/
standard
/
SRCJ
/
Engine.java
< prev
next >
Wrap
Text File
|
2002-05-17
|
22KB
|
937 lines
/**
* title: Engine class (Engine.java)<p>
* description : Controller Panel class <p>
* Copyright (C) 1996-2001 Keio University <p>
* Copyright (C) 1998-2001 Japan Science and Technology Corporation (JST)<p>
* GNU General Public Licence <p>
* Division: Mitui Knowledge Industry Co. Ltd. Bioscience division <p>
* Version : $Id: Engine.java,v 1.6 2002/05/13 00:23:04 ota Exp $ <p>
*/
package ecell;
import java.lang.*;
import java.io.*;
import java.util.*;
class Engine
{
private int e_update_interval = 100;
private int e_flags = 0;
private int e_update_counter = 0;
private boolean e_ready = false;
private boolean e_running = false;
private double e_time_to_stop = -1.0;
private String e_cell_state_name = "";
private String e_script_name = "";
private String e_rule_name = "";
private boolean e_debug = false;
// 2002.3.19 akira shiozawa
private boolean bScriptRunning = false;
private MainWindow window = null;
public static final String DEFAULT_ECS_NAME = "default.ecs";
public static final int CP_ENABLE_START = 0x0001;
public static final int CP_ENABLE_STOP = 0x0002;
public static final int CP_ENABLE_STEP = 0x0004;
public static final int CP_ENABLE_PREFERENCE = 0x0008;
public static final int CP_ENABLE_LOAD_RULE = 0x0010;
public static final int CP_ENABLE_LOAD_SCRIPT = 0x0020;
public static final int CP_ENABLE_LOAD_CS = 0x0040;
public static final int CP_ENABLE_SAVE_CS = 0x0080;
public static final int CP_ENABLE_QUIT = 0x0100;
public static final int CP_ENABLE_TRACER = 0x0200;
public static final int CP_ENABLE_SUBSTANCE = 0x0400;
public static final int CP_ENABLE_REACTOR = 0x0800;
public static String msg_welcome = "";
/**
"E-CELL 2 (C) 1998-2001 Keio university and JST\n" +
"Version 2.12 $Date: 2002/05/13 00:23:04 $\n" +
"This is free software without any warranty under GPL.\n" +
"Please see http://bioinformatics.org/ for detail.\n";
**/
public static String msg_help = "";
// 2002.4 akira shiozawa
private ScriptInterpreter script = null;
/**
* constructor
* @param window MainWindow
*/
public Engine( MainWindow window )
{
setMainWindow( window );
createMessages();
//cp_new();
}
public void startBatch()
{
e_flags &= ~(Engine.CP_ENABLE_START | Engine.CP_ENABLE_STEP);
e_flags |= Engine.CP_ENABLE_STOP;
//cp_enable(e_flags);
e_running = true;
while( e_running )
{
update( this.e_update_interval );
updateInterface();
}
e_flags |= (Engine.CP_ENABLE_START | Engine.CP_ENABLE_STEP);
e_flags &= ~Engine.CP_ENABLE_STOP;
//cp_enable( e_flags );
}
/**
* start simulation
* @return int
*/
public int start()
{
getMainWindow().messageWindow.setDebugMessage( "-----> start@" + getClass().getName() + "()" );
int ret = 1;
e_flags &= ~(Engine.CP_ENABLE_START | Engine.CP_ENABLE_STEP);
e_flags |= Engine.CP_ENABLE_STOP;
//cp_enable( e_flags );
e_running = true;
update( e_update_interval );
updateInterface();
if ( !e_running )
{
e_flags |= (Engine.CP_ENABLE_START | Engine.CP_ENABLE_STEP);
e_flags &= ~Engine.CP_ENABLE_STOP;
//getMinWindow().df.cp_enable( e_flags );
ret = 0;
}
getMainWindow().messageWindow.setDebugMessage( "<----- start@" + getClass().getName() + "( ret:" + ret + " )" );
return ret;
}
/**
* do the simulation in 1 step cycle
* @return int
*/
public int step()
{
return update( 1 );
}
/**
* calculate the system in specified steps
* @param steps int
* @return int
*/
public int update( int steps )
{
getMainWindow().messageWindow.setDebugMessage( "-----> update@" + getClass().getName() + "( steps:" + steps + " )" );
//System.out.println( "-----> update (time_to_stop:" + e_time_to_stop + ")" );
int ret = getMainWindow().df.calc( steps, this.e_time_to_stop );
switch( ret )
{
case 1: // ECS2_CALC_STEP
break;
case 2: //ECS2_CALC_TIME
//e_time_to_stop = MAX_DOUBLE;
setTimeToStop( Double.MAX_VALUE );
stop();
//cp_stop();
getMainWindow().df.stopThread();
/**
// 2002.3.19 akira shiozawa
if( getScriptRunning() )
{
ScriptInterpreter.load_script( this, "" );
}
**/
break;
case 3: //ECS2_CALC_FULL
//dg_tmp_full();
getMainWindow().df.showMessage1( "E-CELL2 : disk full for temprary file.\n" );
stop();
//cp_stop();
getMainWindow().df.stopThread();
/**
// 2002.3.19 akira shiozawa
if( getScriptRunning() )
{
ScriptInterpreter.stop_script( this );
}
**/
break;
case 4: //ECS2_CALC_ERROR
default:
stop();
//cp_stop();
getMainWindow().df.stopThread();
/**
// 2002.3.19 akira shiozawa
if( getScriptRunning() )
{
ScriptInterpreter.stop_script( this );
}
**/
break;
}
getMainWindow().messageWindow.setDebugMessage( "<----- update@" + getClass().getName() + "( ret:" + ret + " )" );
return ret;
}
/**
* update all interfaces
* @return void
*/
public void updateInterface()
{
getMainWindow().messageWindow.setDebugMessage( "-----> updateInterface@" + getClass().getName() + "()" );
getMainWindow().df.updateInterface();
//cp_update(ecs2_get_time());
getMainWindow().messageWindow.setDebugMessage( "<----- updateInterface@" + getClass().getName() + "()" );
}
/**
* ready to go
* @return void
*/
public void readyToGo()
{
e_flags = (CP_ENABLE_START | CP_ENABLE_STEP | CP_ENABLE_PREFERENCE |
CP_ENABLE_LOAD_SCRIPT | CP_ENABLE_LOAD_CS | CP_ENABLE_SAVE_CS |
CP_ENABLE_QUIT | CP_ENABLE_TRACER | CP_ENABLE_SUBSTANCE | CP_ENABLE_REACTOR);
//cp_enable(e_flags);
}
/**
* startup the system
* @return void
*/
public void startup()
{
getMainWindow().messageWindow.setDebugMessage( "-----> startup@" + getClass().getName() + "()" );
loadDefaultScript();
getMainWindow().messageWindow.setDebugMessage( "<----- startup@" + getClass().getName() + "()" );
}
/**
* load specified cell state file
* @param filename String
* @return void
*/
public void loadCellState( String filename )
{
getMainWindow().messageWindow.setDebugMessage( "-----> loadCellState@" + getClass().getName() + "( filename:" + filename + " )" );
e_cell_state_name = filename;
updateFileName();
getMainWindow().df.loadCs( this.e_cell_state_name );
getMainWindow().messageWindow.setDebugMessage( "<----- loadCellState@" + getClass().getName() + "()" );
}
/**
* save cell state to the specified file
* @param filename String
* @return void
*/
public int saveCellState( String filename )
{
getMainWindow().messageWindow.setDebugMessage( "-----> saveCellState@" + getClass().getName() + "( filename:" + filename + " )" );
e_cell_state_name = filename;
int ret = getMainWindow().df.saveCs( this.e_cell_state_name );
getMainWindow().messageWindow.setDebugMessage( "<----- saveCellState@" + getClass().getName() + "()" );
return ret;
}
/**
* load specified rule file
* @param filename String
* @return int
*/
public boolean loadRule( String filename )
{
getMainWindow().messageWindow.setDebugMessage( "-----> loadRule@" + getClass().getName() + "( filename:" + filename + " )" );
e_rule_name = filename;
updateFileName();
getMainWindow().df.loadRule( filename );
boolean ret = ready();
getMainWindow().setRuleFlag( ret );
if( ret && e_time_to_stop < 0.0 )
{
setTimeToStop( Double.MAX_VALUE );
}
getMainWindow().messageWindow.setDebugMessage( "<----- loadRule@" + getClass().getName() + "( ret:" + ret + " )" );
return ret;
}
/**
* update file names
* @return void
*/
public void updateFileName()
{
//cp_name(e_rule_name.c_str(), e_script_name.c_str(), e_cell_state_name.c_str(), e_cs_time);
}
/**
* load the specified script file
* @param filename String
* @return void
*/
public void loadScript( String filename )
{
getMainWindow().messageWindow.setDebugMessage( "-----> loadScript@" + getClass().getName() + "( filename:" + filename + " )" );
this.e_script_name = filename;
this.updateFileName();
// 2002.4 akira shiozawa
if( !getScriptRunning() )
{
getMainWindow().df.saveScriptFileName( filename );
this.script = new ScriptInterpreter( this );
// 2002.5
getMainWindow().setScriptFileName( filename );
}
int ret = script.load_script( /*this,*/ filename );
if( ret == 0 )
{
stopScript();
stop();
}
//int ret = ScriptInterpreter.load_script( this, filename );
getMainWindow().setScriptFlag( (ret == 0)? true : false, e_running );
getMainWindow().messageWindow.setDebugMessage( "<----- loadScript@" + getClass().getName() + "()" );
}
/**
* release shared objects and quitting the system
* @return void
*/
public void quit()
{
getMainWindow().df.releaseReactor();
//exit( 0 );
}
/**
* load the defaut script file
* @return void
*/
public void loadDefaultScript()
{
getMainWindow().messageWindow.setDebugMessage( "-----> loadDefaultScript@" + getClass().getName() + "()" );
String filename =
(e_script_name.equals( "" ))? Engine.DEFAULT_ECS_NAME : this.e_script_name;
File fl = new File( filename );
if( fl.isFile() )
{
//*theMessageWindow << "Script file [" << filename <<
//"] found. executing the script..\n";
loadScript( filename );
}
else
{
//*theMessageWindow << "Script file ["<< filename <<
//"] not found or cannot read it.\n";
}
getMainWindow().messageWindow.setDebugMessage( "<----- loadDefaultScript@" + getClass().getName() + "()" );
}
/**
* create the help message strings
* @return void
*/
private void createMessages()
{
StringBuffer buf = new StringBuffer();
// create help message string
buf.append( "Options :\n" );
buf.append( " -a \t\tset user default accumulator.\n" );
buf.append( " -c \t\tload save time of .cs file as well. (start up time only)\n" );
buf.append( " -d \t\tdebug mode.\n" );
buf.append( " -f FILE\t\trun a E-CELL Script(.ecs) file\n" );
//#ifdef NOT_IMPLEMENTED
//#ifdef _Windows
if( System.getProperty( "os.name" ).toLowerCase().indexOf( "windows" ) > 0 )
{
buf.append( " -r PATH\t\tsemicolon-separated search path to reactors\n" );
}
//#else
else
{
buf.append( " -r PATH\t\tcolon-separated search path to reactors\n" );
}
//#endif /* _Windows */
//#endif /* NOT_IMPLEMENTED */
buf.append( " -t PATH\t\ttemporary file directory\n" );
buf.append( " -u INTEGER\t\tset default update depth.\n" );
buf.append( " -h \t\tprint this message\n" );
Engine.msg_help = buf.toString();
// 2002.1.24 create welcome message string
buf = new StringBuffer();
buf.append( "E-CELL 2 (C) 1998-2002 Keio university and JST\n" );
buf.append( "Version " );
buf.append( window.df.getCoreVersion() );
buf.append( "\n" );
buf.append( "This is free software without any warranty under GPL.\n" );
buf.append( "Please see http://bioinformatics.org/project/?group_id=49 for detail.\n" );
Engine.msg_welcome = buf.toString();
}
/**
* stop running
* @return void
*/
public void stop()
{
this.setRunningStatus( false );
//getMainWindow().stopAction();
}
/**
* shutdown the system with specified exit code
* @param code int
* @return void
*/
public void exit( int code )
{
System.exit( code );
}
/**
* rescue the system
* @return void
*/
public void rescue()
{
getMainWindow().df.rescue();
}
/**
* return the status of system
* @return boolean
*/
public boolean ready()
{
return getMainWindow().df.ready();
}
/**
* get the parent object
* @return MainWindow
*/
public MainWindow getMainWindow()
{
return window;
}
/**
* set the parent object
* @param window MainWindow
* @return void
*/
public void setMainWindow( MainWindow window )
{
this.window = window;
}
/**
* set e_time_to_stop
* @param time_to_stop double
* @return void
*/
public void setTimeToStop( double time_to_stop )
{
this.e_time_to_stop = time_to_stop;
//System.out.println( "setTimeToStop (time_to_stop:" + e_time_to_stop + ")" );
}
/**
* set the update interval
* @param update_interval int
* @return void
*/
public void setUpdateInterval( int update_interval )
{
this.e_update_interval = update_interval;
}
/**
* get the update interval
* @return int
*/
public int getUpdateInterval()
{
return this.e_update_interval;
}
/**
* set the step interval
* @param step_interval double
* @return void
*/
public void setStepInterval( double step_interval )
{
getMainWindow().df.setDelta( step_interval, 0 );
}
/**
* get the step interval
* @return double
*/
public double getStepInterval()
{
return getMainWindow().df.getDelta();
}
/**
* set current time
* @param current_time double
* @return void
*/
public void setCurrentTime( double current_time )
{
getMainWindow().df.setTime( current_time );
}
/**
* get current time in system
* @return double
*/
public double getCurrentTime()
{
return getMainWindow().df.getTime();
}
/**
* set the script file name
* @param script_name String
* @return void
*/
public void setScriptName( String script_name )
{
this.e_script_name = script_name;
}
/**
* get the script file name
* @return String
*/
public String getScriptName()
{
return e_script_name;
}
/**
* set the rule file name
* @param rule_name String
* @return void
*/
public void setRuleName( String rule_name )
{
this.e_rule_name = rule_name;
}
/**
* get the rule file name
* @return String
*/
public String getRuleName()
{
return e_rule_name;
}
/**
* set the cell state file name
* @param cell_state_name String
* @return void
*/
public void setCellStateName( String cell_state_name )
{
this.e_cell_state_name = cell_state_name;
}
/**
* get the cell state file name
* @return String
*/
public String getCellStateName()
{
return e_cell_state_name;
}
/**
* set running status
* @param flag boolean
* @return void
*/
public void setRunningStatus( boolean flag )
{
e_running = flag;
}
/**
* get running status
* @return boolean
*/
public boolean getRunningStatus()
{
return e_running;
}
/**
* When a button of MainWindow is pushed, CORE reacts with the corresponding behavior.
* @param type Window button 0 Start 1 Stop 2 Step
* @return int
*/
public int setMainWindowState( int type )
{
getMainWindow().messageWindow.setDebugMessage( "-----> setMainWindowState@" + getClass().getName() + "( type:" + type + " )" );
int ret = 0;
switch( type )
{
case 0: //
ret = start();
break;
case 1: //
stop();
setTimeToStop( Double.MAX_VALUE );
break;
case 2: //
step();
break;
case -1: //
quit();
break;
}
getMainWindow().messageWindow.setDebugMessage( "<----- setMainWindowState@" + getClass().getName() + "( ret:" + ret + " )" );
return ret;
}
/**
* Pass Param to CORE when the software starts up.
* @param args arg params
* @return boolean <code>true</code> rule file was opened
* <code>false</code> rule file wasn't opened
*/
public boolean setMainWindowsArg( String[] args )
{
getMainWindow().messageWindow.setDebugMessage( "-----> setMainWindowsArg@" + getClass().getName() + "( args.length:" + args.length + " )" );
boolean ret = initialize( args );
getMainWindow().messageWindow.setDebugMessage( "<----- setMainWindowsArg@" + getClass().getName() + "( ret:" + ret + " )" );
return ret;
}
/**
* set preferences
* @param step_interval step interval
* @param update_interval update interval
* @return void
*/
public void setPreference( double step_interval, int update_interval )
{
setStepInterval( step_interval );
setUpdateInterval( update_interval );
}
/**
* Get the Preference Step value
* @return double
*/
public double getPreferenceStep()
{
return getStepInterval();
}
/**
* Get the preference update interval value
* @return int
*/
public int getPreferenceUpdate()
{
return getUpdateInterval();
}
/**
* Process Rule file (Load Rule)
* @param file_path path to rule file
* @return boolean
*/
public boolean setRuleFile( String file_path )
{
return loadRule( file_path );
//return ready();
}
/**
* Process Script file (Load Script).
* @param file_path path to script file
* @return boolean
*/
public boolean setScriptFile( String file_path )
{
getMainWindow().messageWindow.setDebugMessage( "-----> setScriptFile@" + getClass().getName() + "()" );
loadScript( file_path );
getMainWindow().messageWindow.setDebugMessage( "<----- setScriptFile@" + getClass().getName() + "()" );
return ready();
}
/**
* Process CellState file (Load Cell State)
* @param file_path path to cell state file
* @return void
*/
public void setCellStateFile( String file_path )
{
loadCellState( file_path );
}
/**
* Process CellState file (Save Cell State).
* @param file_path path to cell state file
* @return void
*/
public void saveCellStateFile( String file_path )
{
saveCellState( file_path );
}
public void setScriptRunning( boolean status )
{
bScriptRunning = status;
//System.out.println( "-----> setScriptRunning@" + getClass().getName() + "(bScriptRunning:" + bScriptRunning + ")" );
}
public boolean getScriptRunning()
{
return bScriptRunning;
}
public void stopScript()
{
setScriptRunning( false );
script = null;
// 2002.5
//getMainWindow().setScriptFileName( "" );
}
/**
* analyze the program boot parameters
* @param args String[]
* @return boolean
*/
private boolean initialize( String[] args )
{
getMainWindow().messageWindow.setDebugMessage( "-----> initialize@" + getClass().getName() + "( args.length:" + args.length + " )" );
System.out.println( Engine.msg_welcome );
// if (the_engine == NULL) {
// the_engine = new engine_c();
// }
Properties pp = getArgList( args );
for( Enumeration enum = pp.propertyNames(); enum.hasMoreElements(); )
{
String key = (String)enum.nextElement();
if( key.equals( "a" ) ||
key.equals( "c" ) ||
key.equals( "d" ) ||
key.equals( "r" ) ||
key.equals( "t" ) ||
key.equals( "u" ) )
{
continue;
}
else if( key.equals( "f" ) )
{
this.getMainWindow().messageWindow.setDebugMessage( "script file name:" + pp.getProperty( key ) );
setScriptName( pp.getProperty( key ) );
}
else if( key.equals( "D" ) )
{
getMainWindow().messageWindow.setDebugFlag( true );
this.e_debug = true;
}
else
{
System.out.println( Engine.msg_help );
exit( 1 );
}
}
getMainWindow().df.initCore( args.length, mergeStringArgs( args ) );
startup();
getMainWindow().messageWindow.setDebugMessage( "<----- initialize@" + getClass().getName() + "()" );
return ready();
}
/**
* analyze startup args and set into Properties
* @param args String[]
* @return Properties
*/
private Properties getArgList( String[] args )
{
Properties pp = new Properties();
String key = null;
String value = "";
for( int i = 0; i < args.length; i++ )
{
if( args[i].trim().startsWith( "-" ) )
{
key = args[i].substring( args[i].indexOf( '-' ) + 1 );
}
else
{
value = args[i];
}
if( key != null )
{
pp.setProperty( key, value );
value = "";
}
}
return pp;
}
private String mergeStringArgs( String[] args )
{
StringBuffer buf = new StringBuffer();
for( int i = 0; i < args.length; i++ )
{
buf.append( args[i] );
buf.append( "\n" );
}
return buf.toString();
}
public void debugFunc( String str )
{
/**
if( e_debug )
{
System.out.println( "JAVA::" +str );
}
**/
getMainWindow().messageWindow.setDebugMessage( str );
}
public Object[] getMsgWelcome()
{
Object[] obj = new Object[3];
try
{
// 2002.1.24 create welcome message string
StringBuffer buf = new StringBuffer();
buf.append( "E-CELL 2 (C) 1998-2002 Keio university and JST\n" );
buf.append( "Version " );
buf.append( window.df.getCoreVersion() );
buf.append( "\n" );
buf.append( "This is free software without any warranty under GPL.\n" );
buf.append( "Please see " );
java.net.URL url = new java.net.URL( "http://bioinformatics.org/project/?group_id=49" );
String str = "for detail.\n";
obj[0] = buf.toString();
obj[1] = url;
obj[2] = str;
}
catch( Exception e )
{
getMainWindow().messageWindow.setErrorMessage( e );
}
return obj;
}
}